package com.example.token_localstorage;

import com.auth0.jwt.JWT;
import com.example.token_localstorage.token.Result;
import com.example.token_localstorage.token.ResultUtil;
import com.example.token_localstorage.token.TokenUtil;
import com.example.token_localstorage.u.User;
import com.example.token_localstorage.u.UserRepository;
import com.example.token_localstorage.u.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.example.token_localstorage.token.MD5.getMD5;

@Controller
@CrossOrigin
@RequestMapping(path="/")
public class HelloController {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    UserService userService;

//    @PostMapping("/login2")
//    public @ResponseBody
//    ResultUtil login(@RequestParam String username, @RequestParam String password, HttpServletResponse resp, HttpServletRequest req){
////    public ResultUtil login(String username, String password){   //为什么这个在之前的token_test2上面可以读取参数呢
//        password=getMD5(password);
//        User dbUser = userService.findUserByUsernameAndPassword(username, password);
//        if (dbUser == null) {
//            System.out.println("用户名或密码错误!");
//            return ResultUtil.failure().addObject("err", "用户名或密码错误!");
//        }
//        //获取token
//        String token = TokenUtil.getToken(dbUser);
//        //返回给客户端保存
//        resp.addHeader("token",token);
//        return ResultUtil.success().addObject("token", token).addObject("user",dbUser);
//    }
    @PostMapping("/login2")
    public @ResponseBody
    Result login(@RequestParam String username, @RequestParam String password, HttpServletResponse resp, HttpServletRequest req){
        Result result = new Result();
        password=getMD5(password);
        User dbUser = userService.findUserByUsernameAndPassword(username, password);
        if (dbUser == null) {
            System.out.println("用户名或密码错误!");
            result.setCode(200);
            result.setStatus(500);
            result.setMessage("用户名或密码错误!");
            result.setToken("");

            return result;
        }
        //获取token
        String token = TokenUtil.getToken(dbUser);
        System.out.println("token: "+token);
        String deTokenID = JWT.decode(token).getAudience().get(0);
        System.out.println("userId："+deTokenID);

        //返回给客户端保存
        resp.addHeader("token",token);
        result.setToken(token);
        result.setStatus(200);
        result.setCode(200);
        result.setMessage("登陆成功");
        return result.addObject("user",dbUser);
    }
    @GetMapping(path="/list2")
    public @ResponseBody void hellotoken(HttpServletResponse resp, HttpServletRequest req) throws IOException {

        resp.setContentType("text/html;charset=UTF-8");
        resp.getWriter().write("即将为您返回宿舍列表");

    }

    @GetMapping("/")
    public String index(ModelMap map) {
        // 加入一个属性，用来在模板中读取
        map.addAttribute("host", "欢迎页面");

        // return模板文件的名称，对应src/main/resources/templates/index.html
        return "index";
    }
    @GetMapping("/login_jq")
    public String login_jq(ModelMap map) {
        // 加入一个属性，用来在模板中读取
        map.addAttribute("host", "登录页面");

        // return模板文件的名称，对应src/main/resources/templates/login_hm.html
        return "login_jquary";
    }
    @GetMapping("/userServlet")
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求和响应的乱码
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        //1.获取请求参数
        String username = req.getParameter("username");

        //模拟服务器处理请求需要5秒钟
        /*try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/

        //2.判断姓名是否已注册
        if("zhangsan".equals(username)) {
            resp.getWriter().write("<font color='red'>用户名已注册</font>");
        }else {
            resp.getWriter().write("<font color='green'>用户名可用</font>");
        }
    }

}